package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.mapper.GradeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGradeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class GradeServiceImpl implements IGradeService {

    @Autowired
    private GradeMapper gradeMapper;

    //保存和更新员工操作
    public int saveOrUpdate(Grade record) {

        record.setState(false);
        if (record.getId() == null) {
            record.setTeacher(null);
            return gradeMapper.insert(record);
        } else {
            return gradeMapper.updateByPrimaryKey(record);
        }
    }

    //查询单个员工操作
    public Grade selectByPrimaryKey(Long id) {
        return gradeMapper.selectByPrimaryKey(id);
    }


    //员工列表操作
    public List<Grade> selectAll() {
        return gradeMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = gradeMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Grade> data = gradeMapper.queryList(qo);
        return new PageResult(count, data);
    }

    //改变部门的状态
    public void changeState(Long id) {
        gradeMapper.changeState(id);
    }


    @Override
    public Grade getByClassName(String className) {
        return gradeMapper.selectByClassName(className);
    }
    public List<Grade> selectByRoomId(Long roomId) {
        return gradeMapper.selectByRoomId(roomId);
    }

    @Override
    public Grade selectByClassroomId(Long id) {
        return gradeMapper.selectByClassId(id);
    }

    @Override
    public void delete(Long gradeId) {
        gradeMapper.deleteByPrimaryKey(gradeId);
    }

    @Override
    public void updateTeacher(Long sid,Long tid) {
       gradeMapper.updateTeacher(sid,tid);
    }

    //判断班级是否有教室
    public List<Grade> selectByClassId(Long id) {
        return gradeMapper.selectByRoomId(id);
    }

}
